package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by Administrator on 2019/7/8.
 */
@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService{
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail,String tags) {
        //关联目的地
        //其实明细对象总是有攻略对象,但是这个时候的攻略对象中的参数还没有被填充
        //所以我们需要将攻略对象的id给到攻略mapper,让他去查询,就可以获取目的地对象了
        Strategy strategy = strategyMapper.selectByPrimaryKey(strategyDetail.getStrategy().getId());
        Destination dest = strategy.getDest();
        strategyDetail.setDest(dest);
        //作者
        //别问,问就是jy
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname("jy");
        strategyDetail.setAuthor(userInfo);
        //摘要
        //截取200字
        String content = strategyDetail.getStrategyContent().getContent();
        if (content.length()>200){
            strategyDetail.setSummary(content.substring(0,200));
        }else {
            strategyDetail.setSummary(content);
        }
        //是否为海外
        Destination destination = destinationService.getToasts(strategyDetail.getDest().getId()).get(0);
        if (destination.getId()==1L){
            strategyDetail.setIsabroad(StrategyDetail.ABROAD_NO);
        }else{
            strategyDetail.setIsabroad(StrategyDetail.ABROAD_YES);
        }

        if (strategyDetail.getId()==null){
            //创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            //添加内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }


        //如果是编辑,就会存在删不掉
        //那么我们不管是新增还是编辑,都将之前的中间表的关系删除,然后再新增关系
        //这种先删后增,就达到了更新的效果
        strategyTagMapper.deleteRelation(strategyDetail.getId());
        //处理标签,将标签保存到数据库中
        String[] tagArr = tags.split(",");
        System.err.println(tags);
        //为了避免标签重复,我们用set集合去重
        Set<String> tagSet = new HashSet<>();
        List<String> list = Arrays.asList(tagArr);
        tagSet.addAll(list);
        for (String tag : tagSet) {
            StrategyTag strategyTag = new StrategyTag();
            strategyTag.setName(tag);
            strategyTag.setState(StrategyTag.STATE_NORMAL);
            //为了避免重复插入,我们插入之前判断一下这个标签在数据库中是否含有
            strategyTagMapper.insert(strategyTag);
            strategyTagMapper.insertRelation(strategyTag.getId(),strategyDetail.getId());
        }
    }

    @Override
    public List<StrategyDetail> getDetailTop3(Long id) {
        return strategyDetailMapper.selectListTop3(id);
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void updateStatisData(StrategyStatisVO vo) {
        strategyDetailMapper.updateStatisData(vo);
    }

    @Override
    public void delete(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public StrategyDetail get(Long rid) {
        return strategyDetailMapper.selectByPrimaryKey(rid);
    }

    @Override
    public void changeState(StrategyDetail strategyDetail) {
        strategyDetailMapper.updateStateById(strategyDetail);
    }
}
